<template>
  <div>
    <el-tabs v-model="data.activeTab">
      <el-tab-pane label="账户策略" name="account"></el-tab-pane>
      <el-tab-pane label="审核策略" name="audit"></el-tab-pane>
      <el-tab-pane label="安全选项" name="safe"></el-tab-pane>
    </el-tabs>
    <div class="operation" v-if="treeStore.treeDeviceId">
      <div v-if="!data.isVerify">
        <el-button @click="handleSet('reinforceAll')" :disabled="data.isCustom">
          一键加固
        </el-button>
        <el-button @click="handleSet('resetAll')" :disabled="data.isCustom">
          恢复原始
        </el-button>
        <el-button @click="data.isCustom = !data.isCustom"> 自定义 </el-button>
      </div>
      <div v-else>
        <el-button :icon="Back" @click="goBack">返回</el-button>
      </div>
      <div><el-button @click="examine">基线核查</el-button></div>
    </div>

    <div
      class="setting-box formWarp outreach-box"
      v-loading="loading"
      element-loading-text="Loading..."
      element-loading-background="rgba(122, 122, 122, 0.1)"
    >
      <el-form
        :model="data.form"
        ref="form"
        label-width="400px"
        class="clusterManagementForm"
        label-position="left"
      >
        <div v-show="data.activeTab == 'account'">
          <el-form-item label="密码必须符合复杂性要求" prop="psc">
            <el-switch
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              v-model="data.form.account.psc"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
            <div
              v-else
              :class="data.form.account.psc === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.account.psc === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>
          <el-form-item label="密码长度最小值" prop="mpl">
            <el-input
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              class="short"
              v-model.number="data.form.account.mpl"
              type="number"
              min="0"
              max="14"
            >
            </el-input>
            <div
              v-else
              :class="data.form.account.mpl === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.account.mpl === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>
          <el-form-item label="密码最长使用期限" prop="mpa">
            <el-input
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              class="short"
              v-model.number="data.form.account.mpa"
              type="number"
              min="0"
              max="999"
            >
            </el-input>
            <div
              v-else
              :class="data.form.account.mpa === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.account.mpa === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>
          <el-form-item label="强制密码历史" prop="phs">
            <el-input
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              class="short"
              v-model.number="data.form.account.phs"
              type="number"
              min="0"
              max="24"
            >
            </el-input>
            <div
              v-else
              :class="data.form.account.phs === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.account.phs === 0 ? "不符合" : "符合" }}
            </div>
            <span v-show="!data.isVerify" class="colorC2">个记住的密码</span>
          </el-form-item>
          <el-form-item label="账户锁定阈值" prop="lod">
            <el-input
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              class="short"
              v-model.number="data.form.account.lod"
              type="number"
              min="1"
              max="999"
            >
            </el-input>
            <div
              v-else
              :class="data.form.account.lod === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.account.lod === 0 ? "不符合" : "符合" }}
            </div>
            <span v-show="!data.isVerify" class="colorC2">次无效登录</span>
          </el-form-item>
          <el-form-item label="账户：来宾账户状态" prop="ega">
            <el-switch
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              v-model="data.form.account.ega"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
            <div
              v-else
              :class="data.form.account.ega === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.account.ega === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>
        </div>

        <div v-show="data.activeTab == 'audit'">
          <el-form-item label="审核策略更改" prop="saccount">
            <div v-if="!data.isVerify">
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.saccount_success"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.saccount =
                    data.saccount_success + '' + data.saccount_fail
                "
              >
              </el-switch>
              <span class="desc">成功</span>
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.saccount_fail"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.saccount =
                    data.saccount_success + '' + data.saccount_fail
                "
              >
              </el-switch>
              <span class="desc">失败</span>
            </div>
            <div
              v-else
              :class="data.form.audit.saccount === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.audit.saccount === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>

          <el-form-item label="审核登录事件" prop="slogin">
            <div v-if="!data.isVerify">
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.slogin_success"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.slogin =
                    data.slogin_success + '' + data.slogin_fail
                "
              >
              </el-switch>
              <span class="desc">成功</span>
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.slogin_fail"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.slogin =
                    data.slogin_success + '' + data.slogin_fail
                "
              >
              </el-switch>
              <span class="desc">失败</span>
            </div>
            <div
              v-else
              :class="data.form.audit.slogin === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.audit.slogin === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>

          <el-form-item label="审核对象访问" prop="sobject">
            <div v-if="!data.isVerify">
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.sobject_success"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.sobject =
                    data.sobject_success + '' + data.sobject_fail
                "
              >
              </el-switch>
              <span class="desc">成功</span>
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.sobject_fail"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.sobject =
                    data.sobject_success + '' + data.sobject_fail
                "
              >
              </el-switch>
              <span class="desc">失败</span>
            </div>
            <div
              v-else
              :class="data.form.audit.sobject === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.audit.sobject === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>

          <el-form-item label="审核特权使用" prop="squan">
            <div v-if="!data.isVerify">
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.squan_success"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.squan =
                    data.squan_success + '' + data.squan_fail
                "
              >
              </el-switch>
              <span class="desc">成功</span>
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.squan_fail"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.squan =
                    data.squan_success + '' + data.squan_fail
                "
              >
              </el-switch>
              <span class="desc">失败</span>
            </div>
            <div
              v-else
              :class="data.form.audit.squan === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.audit.squan === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>

          <el-form-item label="审核系统事件" prop="ssys">
            <div v-if="!data.isVerify">
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.ssys_success"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.ssys = data.ssys_success + '' + data.ssys_fail
                "
              >
              </el-switch>
              <span class="desc">成功</span>
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.ssys_fail"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.ssys = data.ssys_success + '' + data.ssys_fail
                "
              >
              </el-switch>
              <span class="desc">失败</span>
            </div>
            <div
              v-else
              :class="data.form.audit.ssys === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.audit.ssys === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>

          <el-form-item label="审核账户登录事件" prop="sloginevent">
            <div v-if="!data.isVerify">
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.sloginevent_success"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.sloginevent =
                    data.sloginevent_success + '' + data.sloginevent_fail
                "
              >
              </el-switch>
              <span class="desc">成功</span>
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.sloginevent_fail"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.sloginevent =
                    data.sloginevent_success + '' + data.sloginevent_fail
                "
              >
              </el-switch>
              <span class="desc">失败</span>
            </div>
            <div
              v-else
              :class="data.form.audit.sloginevent === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.audit.sloginevent === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>

          <el-form-item label="审核账户管理" prop="sdir">
            <div v-if="!data.isVerify">
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.sdir_success"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.sdir = data.sdir_success + '' + data.sdir_fail
                "
              >
              </el-switch>
              <span class="desc">成功</span>
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.sdir_fail"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.sdir = data.sdir_success + '' + data.sdir_fail
                "
              >
              </el-switch>
              <span class="desc">失败</span>
            </div>
            <div
              v-else
              :class="data.form.audit.sdir === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.audit.sdir === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>

          <el-form-item label="进程跟踪" prop="sprocess">
            <div v-if="!data.isVerify">
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.sprocess_success"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.sprocess =
                    data.sprocess_success + '' + data.sprocess_fail
                "
              >
              </el-switch>
              <span class="desc">成功</span>
              <el-switch
                :disabled="!data.isCustom"
                v-model="data.sprocess_fail"
                :active-value="1"
                :inactive-value="0"
                @change="
                  data.form.audit.sprocess =
                    data.sprocess_success + '' + data.sprocess_fail
                "
              >
              </el-switch>
              <span class="desc">失败</span>
            </div>
            <div
              v-else
              :class="data.form.audit.sprocess === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.audit.sprocess === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>
        </div>

        <div v-show="data.activeTab == 'safe'">
          <el-form-item class="secure-width" label="交互式登陆：无需按Ctrl+Alt+Del" prop="sctrl">
            <el-switch
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              v-model="data.form.sec.sctrl"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
            <div
              v-else
              :class="data.form.sec.sctrl === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.sec.sctrl === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>
          <el-form-item class="secure-width" label="交互式登陆：不显示上次登录" prop="slastlogin">
            <el-switch
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              v-model="data.form.sec.slastlogin"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
            <div
              v-else
              :class="data.form.sec.slastlogin === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.sec.slastlogin === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>
          <el-form-item class="secure-width" label="网络访问：不允许SAM账户的匿名枚举" prop="ssam">
            <el-switch
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              v-model="data.form.sec.ssam"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
            <div
              v-else
              :class="data.form.sec.ssam === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.sec.ssam === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>
          <el-form-item class="secure-width" label="网络访问：不允许SAM账户和共享匿名枚举" prop="ssams">
            <el-switch
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              v-model="data.form.sec.ssams"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
            <div
              v-else
              :class="data.form.sec.ssams === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.sec.ssams === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>
          <el-form-item class="secure-width" label="关机：清除虚拟内存页面文件" prop="sclean">
            <el-switch
              v-if="!data.isVerify"
              :disabled="!data.isCustom"
              v-model="data.form.sec.sclean"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
            <div
              v-else
              :class="data.form.sec.sclean === 0 ? 'bfh' : 'fh'"
              class="vrify"
            >
              {{ data.form.sec.sclean === 0 ? "不符合" : "符合" }}
            </div>
          </el-form-item>
        </div>
      </el-form>
      <div class="footer" v-if="data.isCustom" style="margin-bottom: 15px">
        <el-button type="primary" @click="submit()">确定</el-button>
        <el-button type="normal" @click="cancel()">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from "vue";
import { useTreeStore } from "~/utils/pinia/tree.js";
import { ElMessage } from "element-plus";
import { Back } from "@element-plus/icons-vue";
import {
  req_getBaselineConf,
  req_setBaselineConf,
  req_reinforceBaseline,
  req_resetBaseline,
  req_verificationBaseline,
} from "~/utils/axios/index.js";
import { _debounce } from "~/utils/common/function.js";
import { eventBus } from "~/utils/common/eventBus.js";
eventBus.on("getStrategyDetail", (detail) => {
  treeStore.openTreeFlag = false;
  parseData(detail.data);
});
const treeStore = useTreeStore();
const loading = ref(false);
const data = reactive({
  activeTab: "account",
  isCustom: false,
  isVerify: false,
  form: {
    sec: {
      sclean: 0,
      sctrl: 0,
      slastlogin: 0,
      ssam: 1,
      ssams: 0,
      sshare: 0,
    },
    audit: {
      saccount: 0,
      sdir: 0,
      slogin: 0,
      sloginevent: 0,
      sobject: 0,
      sprocess: 0,
      squan: 0,
      ssys: 0,
    },
    account: {
      ega: 0,
      lod: 0,
      mpa: 0,
      mpl: 0,
      phs: 0,
      psc: 0,
    },
  },
  saccount_success: "",
  saccount_fail: "",
  sdir_success: "",
  sdir_fail: "",
  slogin_success: "",
  slogin_fail: "",
  sloginevent_success: "",
  sloginevent_fail: "",
  sobject_success: "",
  sobject_fail: "",
  sprocess_success: "",
  sprocess_fail: "",
  squan_success: "",
  squan_fail: "",
  ssys_success: "",
  ssys_fail: "",
});
watch(
  () => treeStore.treeDeviceId,
  () => {
    init();
  }
);
onMounted(() => {
  init();
});
const init = async () => {
  if (!treeStore.treeDeviceId) return;
  loading.value = true;
  try {
    const { data: res } = await req_getBaselineConf(treeStore.treeDeviceId);
    loading.value = false;
    if (res.status) {
      parseData(res.policy);
    }
  } catch (err) {
    loading.value = false;
  }
};
const parseData = (dat) => {
  data.form = dat;
  Object.keys(dat.audit).forEach((key) => {
    data[key + "_fail"] = dat.audit[key] % 10;
    data[key + "_success"] = parseInt(dat.audit[key] / 10) % 10;
  });
};
const cancel = () => {
  data.isCustom = false;
  init();
};

const submit = _debounce(() => {
  Object.keys(data.form.audit).forEach((key) => {
    if (data.form.audit[key] === "00") {
      data.form.audit[key] = 0;
    } else if (data.form.audit[key] === "01") {
      data.form.audit[key] = 1;
    } else {
      data.form.audit[key] = Number(data.form.audit[key]);
    }
  });
  loading.value = true;
  req_setBaselineConf(treeStore.treeDeviceId, data.form)
    .then((res) => {
      loading.value = false;
      if (res.data.succeed) {
        ElMessage.success("设置成功");
        data.isCustom = false;
      } else {
        ElMessage.error("操作失败");
      }
      //init();
    })
    .catch((err) => {
      loading.value = false;
    });
}, 1000);
const handleSet = (type) => {
  let promise;
  loading.value = true;
  if (type === "reinforceAll") {
    promise = req_reinforceBaseline(treeStore.treeDeviceId);
  } else if (type === "resetAll") {
    promise = req_resetBaseline(treeStore.treeDeviceId);
  }
  promise
    .then((res) => {
      loading.value = false;
      if (res.data.status) {
        ElMessage.success("操作成功");
        parseData(res.data.policy);
        data.activeTab = "account";
      } else {
        ElMessage.error("操作失败");
      }
    })
    .catch((err) => {
      loading.value = false;
    });
};
// 基线核查
const examine = () => {
  data.isVerify = true;
  data.activeTab = "account";
  loading.value = true;
  req_verificationBaseline(treeStore.treeDeviceId)
    .then((res) => {
      loading.value = false;
      if (res.data.status) {
        ElMessage.success("操作成功");
        data.form = res.data.policy;
      } else {
        ElMessage.error("操作失败");
      }
    })
    .catch((err) => {
      loading.value = false;
    });
};
const goBack = () => {
  data.isVerify = false;
  data.activeTab = "account";
  init();
};
</script>

<style lang="scss" scoped>
.operation {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
.clusterManagementForm {
  .short {
    width: 100px;
  }
  .colorC2 {
    margin-left: 10px;
    color: #c2c2c2;
  }
  .desc {
    margin: 0 10px;
  }
  .vrify {
    width: 48px;
    height: 28px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    color: #fff;
    line-height: 28px;
    text-align: center;
    margin-top: 2px;
  }
  .fh {
    background: #54c773;
  }
  .bfh {
    background: #ff9000;
  }
}
.formWarp {
  background-color: #fff;
  padding: 15px 15px 1px 15px;
}
:deep(.secure-width>label){
  width: 325px !important;
}
</style>
